//
//  UserInfo.m
//  pai
//
//  Created by pro on 14-11-5.
//  Copyright (c) 2014年 linjunjing. All rights reserved.
//

#import "UserInfo.h"

@implementation UserInfo
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if (self) {
        self.username = [decoder decodeObjectForKey:@"username"];
        self.password = [decoder decodeObjectForKey:@"password"];
        self.mobile = [decoder decodeObjectForKey:@"mobile"];
        self.points = [[decoder decodeObjectForKey:@"points"]intValue];
        self.money = [[decoder decodeObjectForKey:@"money"]intValue];
    }
    return self;
}

- (id) doLoad
{
    NSString* filepath = [self dataFilePath];
    
    BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:filepath];
    if (isExist==NO) {
        return nil;
    }
    
    NSData* data = [[NSData alloc] initWithContentsOfFile:filepath];
    NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    UserInfo* userinfo = [unarchiver decodeObjectForKey:@"userinfo"];
    [unarchiver finishDecoding];
    
    return userinfo;
}

- (BOOL) doSave
{
    NSString* filepath = [self dataFilePath];
    NSLog(@"filepath:%@",filepath);
    NSMutableData* data = [[NSMutableData alloc] init];
    NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:@"userinfo"];
    [archiver finishEncoding];
    NSLog(@"date:%@",data);
    BOOL success = [data writeToFile:filepath atomically:YES];
    NSLog(@"do save flag:%hhd",success);
    return success;
}

- (void) doDelete
{
    NSString* filepath = [self dataFilePath];
    [[NSFileManager defaultManager] removeItemAtPath:filepath error:nil];
}

- (NSString*) dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    documentDirectory = [documentDirectory stringByAppendingString:@"/Caches/appDate"];
    [self createDirectory:documentDirectory];
    return [documentDirectory stringByAppendingPathComponent:@"userinfo.archiver"];
}

- (void)createDirectory:(NSString *)path
{
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL isDir;
    BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
}

#pragma mark -- NSCoding
- (void)encodeWithCoder:(NSCoder *)encode
{
    [encode encodeObject:self.username forKey:@"username"];
    [encode encodeObject:self.password forKey:@"password"];
    [encode encodeObject:self.mobile forKey:@"mobile"];
    [encode encodeObject:[NSString stringWithFormat:@"%ld",(long)self.points] forKey:@"points"];
    [encode encodeObject:[NSString stringWithFormat:@"%ld",(long)self.money] forKey:@"money"];
}

#pragma mark -- NSCopying

- (id)copyWithZone:(NSZone *)zone
{
    UserInfo* copy = [[[self class] allocWithZone:zone] init];
    copy.username = [self.username copyWithZone:zone];
    copy.password = [self.password copyWithZone:zone];
    copy.mobile = [self.mobile copyWithZone:zone];
    copy.points = [[[NSString stringWithFormat:@"%ld",(long)self.points] copyWithZone:zone]intValue];;
    copy.money = [[[NSString stringWithFormat:@"%ld",(long)self.money] copyWithZone:zone]intValue];
    return copy;
}
@end
